# Create your views here.

from django.http import HttpResponseRedirect, Http404, HttpResponseNotFound, HttpResponsePermanentRedirect
from django.shortcuts import render_to_response

from random import choice
from string import ascii_lowercase, digits

from web.models import Category, Entry, Comment
from web.forms import CommentForm

def is_category(slug):
    if Category.objects.filter(url_name=slug).count() == 1:
        return True
    return False

def is_entry(slug):
    if Entry.objects.filter(url_name=slug, type='E').count() == 1:
        return True
    return False

def is_page(slug):
    if Entry.objects.filter(url_name=slug, type='P').count() == 1:
        return True
    return False

def process_url(request, index=0, slug=None):
    if slug:
        if is_category(slug):
            return show_entries(request, slug, index)

        elif is_entry(slug):
            return show_entry(request, slug)

        elif is_page(slug):
            return show_page(request, slug)

        else:
            return show_404(request)

    else:
        return show_entries(request, slug, index)

def show_404(request):
    entry = Entry.objects.filter(url_name='error-404')[0]
    from django.template import loader
    return HttpResponseNotFound(loader.render_to_string('page.html',
        {
            'page_title': entry.name,
            'page_content': entry.content,
        },)
    )

def show_entries(request, slug=None, index=0):
    index_gap = int(index) + 10

    if slug:
        category = Category.objects.filter(url_name=slug)[0]
        page_title = category.name
        entries = Entry.objects.filter(categories=category.id).order_by('-id')[index:index_gap]

    else:
        page_title = 'Inicio'
        entries = Entry.objects.all().order_by('-id')[index:index_gap]

    if index:
        newer = int(index) - 10
        older = index_gap
    else:
        newer = None
        older = index_gap

    return render_to_response('entries.html',
        {
            'page_title': page_title,
            'rs_entries': entries,
            'newer': newer,
            'older': older,
        },
    )

def show_entry(request, slug):
    if request.method == 'POST':
        comment = Comment()
        comment_form = CommentForm(request.session.get('comment_captcha'), request.POST, comment)

        if comment_form.is_valid():
            commen = comment_form.save()
            return HttpResponseRedirect(request.path)

        else:
            request.session['comment_captcha'] = ''.join(
                [choice(ascii_lowercase + digits) for i in range(5)]
            )

    else:
        request.session['comment_captcha'] = ''.join(
            [choice(ascii_lowercase + digits) for i in range(5)]
        )
        comment_form = CommentForm(request.session.get('comment_captcha'))

    entry = Entry.objects.filter(url_name=slug, type='E')[0]
    comments = Comment.objects.filter(entry=entry)

    return render_to_response('entry.html',
        {
            'page_title': entry.name,
            'entry': entry,
            'rs_comments': comments,
            'form_action_url': request.path,
            'comment_form': comment_form,
        },
    )

def show_page(request, slug):
    entry = Entry.objects.filter(url_name=slug, type='P')[0]

    return render_to_response('page.html',
        {
            'page_title': entry.name,
            'page_content': entry.content,
        },
    )
